﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Ini;

namespace QuickPasteItLib
{
    public class QpiConfig
    {
        /// <summary>
        /// Maps pastebin name strings to their Types
        /// </summary>
        public static readonly Dictionary<string, Type> pasteBinMap = new Dictionary<string, Type>
        {
            {"sprunge"        ,  typeof (  SprungePasteBin        )},
            {"vpaste"         ,  typeof (  VpastePasteBin         )},
            {"pastebin.com"   ,  typeof (  PastebinDotComPasteBin )},
            {"ubuntu"         ,  typeof (  UbuntuPasteBin         )},
        };

        public const string DEFAULT_PASTEBIN = "sprunge";

        /// <summary>
        /// The author to be shown on all pastes.
        /// </summary>
        public string Author { get; set; }

        /// <summary>
        /// The user's preferred pastebin.
        /// </summary>
        public string PreferredPastebin { get; set; }

        /// <summary>
        /// Takes the given user-inputted string and returns the matching IPasteBin object.
        /// </summary>
        /// <param name="input">The user-inputted pastebin name string.</param>
        /// <returns>An IPasteBin object to be used for pasting code.</returns>
        public static IPasteBin GetPasteBin(string input)
        {
            input = input.ToLower().Trim();

            if (pasteBinMap.ContainsKey(input))
                return Activator.CreateInstance(pasteBinMap[input]) as IPasteBin;
            else // silently fall back to default pastebin
                return Activator.CreateInstance(pasteBinMap[QpiConfig.DEFAULT_PASTEBIN]) as IPasteBin;
        }

        /// <summary>
        /// Attempts to retrieve a configuration or creates a new default configuration in memory.
        /// </summary>
        public QpiConfig()
        {
            string homePath = (Environment.OSVersion.Platform == PlatformID.Unix ||
                               Environment.OSVersion.Platform == PlatformID.MacOSX)
                             ? Environment.GetEnvironmentVariable("HOME")
                             : Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");

            // Default configuration:
            Author = "Anonymous";
            PreferredPastebin = "sprunge";

            if (File.Exists(Path.Combine(homePath, "qpi.ini")))
            {
                ParseConfig(File.OpenRead(Path.Combine(homePath, "qpi.ini")));
            }

            else if (File.Exists(Path.Combine(homePath, ".qpirc")))
            {
                ParseConfig(File.OpenRead(Path.Combine(homePath, ".qpirc")));
            }
        }

        private void ParseConfig(Stream configStream)
        {
            IniDocument conf = new IniDocument();
            conf.Load(configStream);
            try { Author = conf.Sections["DEFAULTS"].Parameters["author"].Value.ToString(); }
            catch { }
            try { PreferredPastebin = conf.Sections["DEFAULTS"].Parameters["pastebin"].Value.ToString(); }
            catch { }
        }
    }
}
